package com.yupi.springbootinit.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "spring.redis")   // 引用yml文件中的某一个配置
@Data
public class RedissonConfig {

    // 读取yml文件中的数据
    private Integer database;
    private String host;
    private Integer port;
    private String password;

    @Bean
    public RedissonClient redissonClient(){
        // 创建配置对象
        Config config = new Config();
        config.useSingleServer()
                .setDatabase(database)
                .setAddress("redis://"+host+":"+port);  //redis://127.0.0.1:6379
                // .setPassword(password);
        // 初始化 Async API
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}
